package com.lzgcoding.onlineshopapp.feature.main.dashboard.component

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import coil3.compose.SubcomposeAsyncImage
import com.lzgcoding.onlineshopapp.R
import com.lzgcoding.onlineshopapp.nav.AppRoute
import com.lzgcoding.onlineshopapp.room.entity.ItemEntity
import com.lzgcoding.onlineshopapp.ui.theme.LargePadding
import com.lzgcoding.onlineshopapp.ui.theme.MediumPadding
import com.lzgcoding.onlineshopapp.util.formatCurrency

@Composable
fun PopularList(
    populars: List<ItemEntity>,
    navigateToAppRoute: (AppRoute) -> Unit
) {
    ListItem(
        headlineContent = {
            Text(
                text = stringResource(R.string.str_popular_products),
                style = MaterialTheme.typography.bodyLarge, fontWeight = FontWeight.Bold,
            )
        },
        trailingContent = {
            TextButton(onClick = {}) {
                Text(text = stringResource(R.string.str_see_all))
            }
        }
    )
    LazyRow(
        modifier = Modifier.fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(LargePadding)
    ) {
        item { Spacer(Modifier) }
        items(populars) {
            PopularProductItem(
                pic = it.picUrls.first(),
                name = it.title,
                offPercentage = it.offPercent,
                price = it.price,
                oldPrice = it.oldPrice,
                rating = it.rating,
                onClick = { navigateToAppRoute(AppRoute.Detail(it.id)) }
            )
        }
        item { Spacer(Modifier) }
    }
}

@Composable
private fun PopularProductItem(
    pic: String,
    name: String,
    offPercentage: String,
    price: Double,
    oldPrice: Double,
    rating: Double,
    onClick: () -> Unit
) {
    Card(
        modifier = Modifier.width(200.dp),
        onClick = onClick,
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surfaceContainer
        ),
        elevation = CardDefaults.elevatedCardElevation(defaultElevation = 3.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(MediumPadding)
        ) {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .aspectRatio(1f)
                    .clip(RoundedCornerShape(LargePadding))
                    .background(Color(0xFFe9e9e9))
            ) {
                SubcomposeAsyncImage(
                    model = pic,
                    contentDescription = name,
                    modifier = Modifier
                        .padding(MediumPadding)
                        .fillMaxWidth()
                        .aspectRatio(1f),
                    contentScale = ContentScale.Fit,
                    loading = {
                        Box(
                            modifier = Modifier
                                .padding(MediumPadding)
                                .fillMaxWidth()
                                .aspectRatio(1f),
                            contentAlignment = Alignment.Center
                        ) {
                            CircularProgressIndicator()
                        }
                    }
                )
                Surface(
                    shape = CircleShape,
                    color = Color.Red,
                    contentColor = Color.White,
                    modifier = Modifier
                        .align(Alignment.TopStart)
                        .padding(start = MediumPadding, top = MediumPadding)
                ) {
                    Text(
                        text = offPercentage,
                        modifier = Modifier
                            .padding(horizontal = LargePadding, vertical = MediumPadding)
                            .wrapContentSize(),
                        textAlign = TextAlign.Center,
                        style = MaterialTheme.typography.labelMedium
                    )
                }
            }
            Spacer(Modifier.height(MediumPadding))
            Text(
                text = name,
                style = MaterialTheme.typography.bodyMedium,
                fontWeight = FontWeight.Bold,
                maxLines = 2,
                minLines = 2,
                overflow = TextOverflow.Ellipsis
            )
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(MediumPadding)
            ) {
                Text(
                    text = buildAnnotatedString {
                        withStyle(
                            SpanStyle(
                                textDecoration = TextDecoration.LineThrough,
                                fontSize = MaterialTheme.typography.bodySmall.fontSize,
                            )
                        ) {
                            append(oldPrice.formatCurrency())
                        }
                        append(" ")
                        withStyle(
                            SpanStyle(
                                fontSize = MaterialTheme.typography.bodyLarge.fontSize,
                                color = Color.Red
                            )
                        ) {
                            append(price.formatCurrency())
                        }
                    },
                    modifier = Modifier.weight(1f)
                )
                Text(text = "($rating)", style = MaterialTheme.typography.labelSmall)
                Image(
                    painter = painterResource(R.drawable.star),
                    contentDescription = "star"
                )
            }
        }
    }
}